package com.hoyue.interact.service.impl;


import com.hoyue.common.utils.DateUtils;
import com.hoyue.interact.bean.Matter;
import com.hoyue.interact.bean.UserMatter;
import com.hoyue.interact.dao.MatterMapper;
import com.hoyue.interact.dao.UserMatterMapper;
import com.hoyue.interact.service.MatterService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MatterServiceimpl implements MatterService {

    DateUtils dateUtils;

    @Resource
    MatterMapper matterMapper;
    @Resource
    UserMatterMapper userMatterMapper;


    @Override
    public List<Matter> ListMatter(Integer id) {
        return userMatterMapper.ListMatter(id);
    }
    @Override
    public Matter getMatter(Integer id) {
        return matterMapper.getMatter(id);
    }
    @Override
    public int addMatter(UserMatter userMatter) {
        return userMatterMapper.addMatter(userMatter);
    }
    @Override
    public int insertMatter(String headline, String mdetail, Integer id, String username, String endtime, String starttime, Integer courseid) {

        Date createdate = dateUtils.getDateTime();
        Date start =  dateUtils.getStrToDate(starttime);
        Date end = dateUtils.getStrToDate(endtime);
        Matter matter = new Matter(headline,mdetail,createdate,start,end,username);

        matterMapper.insertMatter(matter);
        return matter.getId();
    }
    @Override
    public int updateMatter(String headline, String mdetail, String endtime, String starttime, Integer id) {
        Date updatetime = dateUtils.getDateTime();
        Date start =  dateUtils.getStrToDate(starttime);
        Date end = dateUtils.getStrToDate(endtime);
        Matter matter=new Matter(headline,mdetail,updatetime,start,end,id);
        return matterMapper.updateMatter(matter);
    }
    @Override
    public int delMatter(int matterId) {
        UserMatter userMatter = new UserMatter(null,matterId,null);
        userMatterMapper.movMatter(userMatter);
        return matterMapper.delMatter(matterId);
    }
    @Override
    public int outMatter(int userId, int matterId, int courseId) {
        UserMatter userMatter = new UserMatter(userId, matterId,courseId);
        return userMatterMapper.movMatter(userMatter);
    }

    @Override
    public int movMatter(UserMatter userMatter) {

        return userMatterMapper.movMatter(userMatter);
    }

    /**
     * 编辑
     * @param matter
     * @return
     */
    @Override
    public Map<String, String> getMatterStr(Matter matter) {
        Map<String,String> map=new HashMap<String,String>();
        map.put("headline",matter.getHeadline());
        map.put("mdetail",matter.getMdetail());
        map.put("starttime",dateUtils.getDateToStr(matter.getStarttime()));
        map.put("endtime",dateUtils.getDateToStr(matter.getEndtime()));
        return map;
    }

    /**
     * 关键字查询
     * @param mdetail
     * @return
     */
    @Override
    public List<Matter> Mfind(String mdetail) {

        return  matterMapper.Mfind(mdetail);
    }

    /**
     * 置顶功能
     * @param id
     * @return
     */
    @Override
    public List<Matter> Istop(Integer id) {
        return matterMapper.Istop(id);
    }

    @Override
    public int Istopupdate(Integer Istop) {

        Matter matter =new Matter(Istop);
        return matterMapper.IstopupdateMatter(matter);
    }

    //根据id查询话题
    @Override
    public List<Matter> Amatter(Integer id) {
        return  userMatterMapper.Amatter(id);
    }
    //根据id查询话题



}
